package com.dingpeikeji.quickdeer.modules.rentproduct.web;

import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dingpeikeji.quickdeer.core.common.config.Global;
import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TRentprodModify;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TRentprodModifyService;

/**
 * 整车价格修改Controller
 * @author wuhao
 * @version 2016-08-08
 */
@Controller
@RequestMapping(value = "${adminPath}/rentprod/tRentprodModify")
public class TRentprodModifyController extends BaseController {

	@Autowired
	private TRentprodModifyService tRentprodModifyService;

	
	@ModelAttribute
	public TRentprodModify get(@RequestParam(required=false) String rentchgid) {
		TRentprodModify entity = null;
		if (StringUtils.isNotBlank(rentchgid)){
			entity = tRentprodModifyService.get(rentchgid);
		}
		if (entity == null){
			entity = new TRentprodModify();
		}
		return entity;
	}
	
	@RequiresPermissions("rentproduct:tRentprodModify:view")
	@RequestMapping("{urlParam}List")
	public String list(@PathVariable String urlParam, TRentprodModify tRentprodModify, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TRentprodModify> page = tRentprodModifyService.findPage(new Page<TRentprodModify>(request, response), tRentprodModify); 
		model.addAttribute("page", page);
		model.addAttribute("tRentprodModify", tRentprodModify);
		return MessageFormat.format("modules/rentproduct/{0}ModifyList", urlParam);
	}
	
	@RequiresPermissions("rentprod:tRentprod:view")
	@RequestMapping("busprodlist")
	public String busprodlist(TRentprodModify tRentprodModify, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TRentprodModify> page = tRentprodModifyService.findPage(new Page<TRentprodModify>(request, response), tRentprodModify); 
		model.addAttribute("page", page);
		model.addAttribute("tRentprodModify", tRentprodModify);
		
		return "modules/rentprod/tRentprodModifyList";
	}

	@RequiresPermissions("rentprod:tRentprod:edit")
	@RequestMapping(value = "modifyform")
	public String modifyform(TRentprodModify tRentprodModify, Model model) {
		// 生效起始时间默认第二天,必须大于当天
		// 生效终止时间默认第二天，不能大于租赁止期
		if(tRentprodModify.getEfftdate()==null && tRentprodModify.getInvaliddate()==null){
			Calendar calendar = Calendar.getInstance();
			calendar.add(Calendar.DAY_OF_MONTH, 1);
			tRentprodModify.setEfftdate(calendar.getTime());
			tRentprodModify.setInvaliddate(calendar.getTime());
		}
/*		TRentproduct tRentproduct = tRentproductService.get(tRentprodModify.getRentid());
		if(tRentprodModify.getRentchgid()==null){
			// 如果是新建则设置默认值
			tRentprodModify.setSuitfreeprice(tRentproduct.getSuitfreeprice());
			tRentprodModify.setSuitonewayprice(tRentproduct.getSuitonewayprice());
			tRentprodModify.setSuitrtnprice(tRentproduct.getSuitrtnprice());
		}*/
		model.addAttribute("tRentprodModify", tRentprodModify);
		model.addAttribute("maxDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
		model.addAttribute("minDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
		return "modules/rentprod/tRentprodModifyForm";
	}

	@RequiresPermissions("rentprod:tRentprod:edit")
	@RequestMapping(value = "save")
	public String save(TRentprodModify tRentprodModify, Model model, RedirectAttributes redirectAttributes) throws Exception {
		if (!beanValidator(model, tRentprodModify)){
			return modifyform(tRentprodModify, model);
		}
		// 生效日期必须大于当天，失效日期必须小于截止日期
/*		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = sdf.parse(sdf.format(new Date()));
		if(tRentprodModify.getEfftdate().getTime()<date.getTime() ||
				tRentprodModify.getInvaliddate().getTime()>tRentproduct.getStopdate().getTime()){
			addMessage(model, "日期范围不正确");
			return modifyform(tRentprodModify, model);
		}*/
		try {
			tRentprodModifyService.save(tRentprodModify);
			addMessage(redirectAttributes, "保存整车调整成功");
		} catch (Exception e) {
			addMessage(redirectAttributes, "保存整车调整失败："+e.getMessage());
			System.out.println(e.getMessage());
		}
		
		return "redirect:"+Global.getAdminPath()+"/rentprod/tRentprodModify/busprodlist?repage&forsaleid="+tRentprodModify.getForsaleid();
	}
	
	@RequiresPermissions("rentproduct:tRentprodModify:edit")
	@RequestMapping(value = "{urlParam}Delete")
	public String delete(@PathVariable String urlParam, TRentprodModify tRentprodModify, RedirectAttributes redirectAttributes) {
		tRentprodModifyService.delete(tRentprodModify);
		addMessage(redirectAttributes, "删除整车调整成功");
		return "redirect:"+Global.getAdminPath()+"/rentprod/tRentprodModify/busprodlist?repage&forsaleid="+tRentprodModify.getForsaleid();
	}

}